package idv.jieh.commons.util;

import java.io.*;
import java.util.Properties;

/**
 * <p>Title: 藍色小惡魔的系統</p>
 * <p>Description: 這個類別用來載入及回存設定。</p>
 *
 * @author Jieh
 * @version 1.0
 */
public class ConfigFile extends Properties {
  private String header = "藍色小惡魔套件設定檔";
  private String fullpath;

  /**
   * 建構子[1]。<p>
   * 指定檔名並配合由 property 取出的路徑，載入相關設定。<p>
   * 2004-12-24 Modify by jieh <br>
   * @param filename 希望讀取的檔案名稱。
   * @throws java.io.IOException
   */
  public ConfigFile(String filename) throws IOException {
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    if (classloader == null) {
      classloader = this.getClass().getClassLoader();
    }

    classloader.getResource("/");

    System.out.println("Debug getClass() : " + getClass());
    fullpath = getClass().getResource("").getPath() + filename;
    fullpath = DataAnalysis.replace(fullpath, "%20", " ");
    File file = createFile();
    FileInputStream fis = new FileInputStream(file);
    java.io.BufferedInputStream bis = new java.io.BufferedInputStream(fis);
    super.load(bis);
    fis.close();
  }

  /**
   * 建構子[2]。<p>
   * 指定路徑及檔名，載入相關設定。<br>
   * @param path 希望讀取檔案的路徑，不含檔名。
   * @param filename 希望讀取的檔案名稱。
   * @throws java.io.IOException
   */
  public ConfigFile(
    String path,
    String filename)
    throws java.io.IOException {

    String separator = System.getProperty("file.separator");
    String replace_str = separator.equals("/") ? "\\" : "/";
    path = DataAnalysis.replace(path, replace_str, separator);

    if (!path.endsWith(separator)) {
      path = path + separator;
    }
    fullpath = path + filename;
    File file = createFile();

    java.io.FileInputStream fis = new java.io.FileInputStream(file);
    java.io.BufferedInputStream bis = new java.io.BufferedInputStream(fis);
    super.load(bis);
    fis.close();
  }

  /**
   * 回存設定。<p>
   */
  public void store() {
    try {
      File file = createFile();
      java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
      super.store(fos, header);
      fos.close();
    } catch (java.io.IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * 設定回存時的 header。<p>
   * @param s header 內容。
   */
  public void setHeader(String s) {
    header = s;
  }

  // 由程式中紀錄的完整路徑開啟一個 File 物件並回傳。
  private File createFile() throws java.io.IOException {
    File file = new java.io.File(fullpath);
    if (!file.exists()) {
      System.out.println("Config File isn't exist...");
      if (file.createNewFile()) {
        System.out.println(fullpath + " Config file created success!!! ");
      }
    }
    return file;
  }


  public static void main(String[] args) throws Exception{
    ConfigFile me1 = new ConfigFile("jieh-test.properties");
    ConfigFile me2 = new ConfigFile("d:/tmp", "jieh-test.properties");
    me2.setProperty("KK","AA");
    me2.store();
  }

} // End Class ConfigFile
